package ${packageName}.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zg.common.core.A;
import ${packageName}.dao.${name};
import ${packageName}.dao.${name}Dao;
import ${packageName}.service.${name}Service;
import ${packageName}.vo.${name}AddVo;
import ${packageName}.vo.${name}BatchVo;
import ${packageName}.vo.${name}UpdateVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.List;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class ${name}ServiceImpl implements ${name}Service {

    private final ${name}Dao ${name?uncap_first}Dao;

    void checkAdd(${name}AddVo param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${name} add(${name}AddVo param) {
        //校验逻辑
        checkAdd(param);
        //创建对象
        ${name} obj = new ${name}();
        //属性处理
        BeanUtil.copyProperties(param, obj);
        StringBuffer strBuff = new StringBuffer();
        <#list likeList as pro>
        strBuff.append(obj.get${pro.name?cap_first}());
        </#list>
        obj.setSearchStr(strBuff.toString());
        //保存对象
        ${name?uncap_first}Dao.save(obj);
        return obj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestParam("ids") List<Long> idList) {
        ${name?uncap_first}Dao.removeBatchByIds(idList);
    }

    void checkUpdate(${name}UpdateVo param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ${name} update(${name}UpdateVo param) {
        //查询对象
        ${name} oldObj = ${name?uncap_first}Dao.getById(param.getId());
        A.isInDatabase(oldObj, "要更新的对象");
        //校验逻辑
        checkUpdate(param);
        //属性处理
        BeanUtil.copyProperties(param, oldObj);
        StringBuffer strBuff = new StringBuffer();
        <#list likeList as pro>
        strBuff.append(oldObj.get${pro.name?cap_first}());
        </#list>
        oldObj.setSearchStr(strBuff.toString());
        //更新对象
        ${name?uncap_first}Dao.updateById(oldObj);
        return oldObj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(${name}BatchVo param) {
    //创建批量更新列表
    List<${name}> updateList = new ArrayList<>();
        for (Long id : param.getIdList()) {
            ${name} oldObj = ${name?uncap_first}Dao.getById(id);
            <#list batchList as pro>
            if(param.get${pro.name?cap_first}Ck()){
                oldObj.set${pro.name?cap_first}(param.get${pro.name?cap_first}());
            }
            </#list>
            oldObj.setSearchStr("");
            updateList.add(oldObj);
        }
        ${name?uncap_first}Dao.updateBatchById(updateList);
    }
<#if hasSortColumn>
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sort(String sortStr) {
        if (StrUtil.isBlank(sortStr)) {
            return;
        }
        String[] strArr = sortStr.split(",");
        for (String str : strArr) {
            String[] sortArr = str.split("_");
            Long id = Long.parseLong(sortArr[0]);
            Integer sort = Integer.parseInt(sortArr[1]);
            ${name} obj = ${name?uncap_first}Dao.getById(id);
            if (obj != null) {
                obj.set${sortColumnName?cap_first}(sort);
                ${name?uncap_first}Dao.updateById(obj);
            }
        }
    }
</#if>
}
